package org.example.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.constant.ExceptionConstant;
import org.example.domain.service.OpenRankServiceDomain;
import org.example.domain.service.RepositoryServiceDomain;
import org.example.entity.Result;
import org.example.vo.CommunityOpenRankVO;
import org.example.vo.ContributorVO;
import org.example.vo.KeyContributorsVO;
import org.example.vo.RepositoryVO;
import org.springframework.web.bind.annotation.*;


/**
 * Description: 控制层
 * Created by 陈宇 on 2024-12-27 14:16
 * Created with IntelliJ IDEA.
 * @author 陈宇
 */
@RestController
@Slf4j
@RequestMapping("/analysis")
public class ApplicationController {

    @Resource
    private RepositoryServiceDomain repositoryServiceDomain;
    @Resource
    private OpenRankServiceDomain openRankServiceDomain;

    /**
     * 根据时间和用户 id，查询该用户各项指标数据
     */
    @PostMapping("/queryContributorByDate")
    public Result<ContributorVO> queryContributorByDate(@RequestBody ContributorVO contributorVO) {
        try {
            return Result.ok(repositoryServiceDomain.queryContributorByDate(contributorVO.getPlatform(), contributorVO.getRepoName(), contributorVO.getUserId(), contributorVO.getStartDate(), contributorVO.getDate()));
        } catch (Exception e) {
            return Result.fail(null);
        }
    }
    /**
     * 按照范围时间获取关键贡献者列表
     */
    @PostMapping("/rangeQueryKeyContributors")
    public Result<KeyContributorsVO> rangeQueryKeyContributors(@RequestBody RepositoryVO repositoryVO) {
        try {
            return Result.ok(repositoryServiceDomain.rangeQueryKeyContributors(repositoryVO.getName(), repositoryVO.getPlatform(), repositoryVO.getStartDate(), repositoryVO.getDate()));
        } catch (Exception e) {
            log.error("project-contributor-analysis:controller:ApplicationController:rangeQueryKeyContributors:Exception: [{}]", e.getMessage());
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    /**
     * 按年获取关键贡献者列表
     */
    @PostMapping("/queryKeyContributorsByYear")
    public Result<KeyContributorsVO> queryKeyContributorsByYear(@RequestBody RepositoryVO repositoryVO) {
        try {
            Preconditions.checkNotNull(repositoryVO, ExceptionConstant.ARGUMENT_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getName() != null && repositoryVO.getName() != "", ExceptionConstant.REPOSITORY_NAME_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getPlatform() != null && repositoryVO.getPlatform() != "", ExceptionConstant.REPOSITORY_PLATFORM_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getDate() != null && repositoryVO.getDate() != "", ExceptionConstant.REPOSITORY_DATE_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getDate().length() == 4, ExceptionConstant.REPOSITORY_DATE_FORMAT_EXCEPTION);
            if (log.isInfoEnabled()) {
                log.info("project-contributor-analysis:controller:ApplicationController:queryKeyContributorsByYear:RepositoryVO: [{}]", JSON.toJSONString(repositoryVO));
            }
            return Result.ok(repositoryServiceDomain.queryKeyContributorsByYear(repositoryVO.getName(), repositoryVO.getPlatform(), repositoryVO.getDate()));
        } catch (Exception e) {
            log.error("project-contributor-analysis:controller:ApplicationController:queryKeyContributorsByMonths:Exception: [{}]", e.getMessage());
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    /**
     * 获取关键贡献者列表按月份（展示 用户名 贡献度 影响力）
     */
    @PostMapping("/queryKeyContributorsByMonth")
    public Result<KeyContributorsVO> queryKeyContributorsByMonth(@RequestBody RepositoryVO repositoryVO) {
        try {
            Preconditions.checkNotNull(repositoryVO, ExceptionConstant.ARGUMENT_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getName() != null && repositoryVO.getName() != "", ExceptionConstant.REPOSITORY_NAME_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getDate() != null && repositoryVO.getDate() != "", ExceptionConstant.REPOSITORY_DATE_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getType() >= 0 && repositoryVO.getType() <= 2, ExceptionConstant.REPOSITORY_TYPE_EXCEPTION);
            if (log.isInfoEnabled()) {
                log.info("project-contributor-analysis:controller:ApplicationController:queryKeyContributorList:RepositoryVO: [{}]", JSON.toJSONString(repositoryVO));
            }
            return Result.ok(repositoryServiceDomain.queryKeyContributorsByMonth(repositoryVO.getName(), repositoryVO.getPlatform(), repositoryVO.getDate(), repositoryVO.getType()));
        } catch (Exception e) {
            log.error("project-contributor-analysis:controller:ApplicationController:queryKeyContributorList:Exception: [{}]", e.getMessage());
            e.printStackTrace();
            return Result.fail(null);
        }
    }

    /**
     * 获取社区 OpenRank
     */
    @PostMapping("/queryCommunityOpenRank")
    public Result<CommunityOpenRankVO> queryCommunityOpenRank(@RequestBody RepositoryVO repositoryVO) {
        try {
            Preconditions.checkNotNull(repositoryVO, ExceptionConstant.ARGUMENT_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getName() != null && repositoryVO.getName() != "", ExceptionConstant.REPOSITORY_NAME_NULL_EXCEPTION);
            Preconditions.checkArgument(repositoryVO.getPlatform() != null && repositoryVO.getPlatform() != "", ExceptionConstant.REPOSITORY_PLATFORM_NULL_EXCEPTION);
            if (log.isInfoEnabled()) {
                log.info("project-contributor-analysis:controller:ApplicationController:queryCommunityOpenRank:RepositoryVO: [{}]", JSON.toJSONString(repositoryVO));
            }
            return Result.ok(openRankServiceDomain.queryCommunityOpenRank(repositoryVO.getPlatform(), repositoryVO.getName()));
        } catch (Exception e) {
            log.error("project-contributor-analysis:controller:ApplicationController:queryCommunityOpenRank:Exception: [{}]", e.getMessage());
            e.printStackTrace();
            return Result.fail(null);
        }
    }

}
